const seller = require("./sellerModule");

function addseller(username,password,logo){
    return new Promise((resolve,reject)=>{
        seller.findOne({sellername:username})
        .then(
            (result)=>{
                if(result){
                    reject("该用户已存在");
                }else{
                    new seller({sellername:username,password,logo}).save().then(
                        (result)=>{
                            if(result){
                                resolve("注册成功");
                            }else{
                                reject("服务器忙，请重试");
                            }
                        }
                    )
                }
            }
        )
    })
}


function findsellerlogin(username,password){
    return new Promise((resolve,reject)=>{
        seller.findOne({sellername:username,password})
        .then(
            (result)=>{
                if(result){
                    resolve(result._id);
                }else{
                    reject("用户名或密码错误");
                }
            }
        )
    })
}

function finduserbyid(id){
    return new Promise((resolve,reject)=>{
        seller.findById(id)
        .then(
            (result)=>{
                if(result){
                    resolve(result);
                }else{
                    reject("未登录");
                }
            }
        )
    })
}

module.exports = {addseller,findsellerlogin,finduserbyid}